<?php
namespace Org\Oa;
/**
 * 订单操作类
 * @author Administrator
 *
 */
class order{
	/**
	 * 
	 * @param unknown $goods 商品信息
	 * $num 购买数量
	 * $type 0 是普通订单 1是限时抢购 2是团购订单
	 */
	public static function add_order($uid,$goods,$num,$type=0){
		$payable_amount = 0; 
		$real_amount = 0;
		//$payable_freight = 0;
		$promotions = 0;
		$point = 0;
		foreach ($goods as $i=>$va){
			$payable_amount += $va['sell_price']*$num[$i];
			if(empty($type)){
				$real_price[$i] = (\Org\Oa\promotion::product_for_promotion($uid,$va['id'],2, 0));
			}else{
				$real_price[$i] = $va['now_price'];
			}
			$real_amount += ($real_price[$i]*$num[$i]);
			$promotions += (($va['sell_price'] - $real_price[$i])*$num[$i]);
			$point += $va['integral'];
			$goods[$i]['sell_price'] =  $real_price[$i];
			$prop[$i] = \Org\Oa\promotion::prefer_for_promotion($uid, $va['id'], 0); 
			if(!empty($prop[$i])){
				$prefer_prop[] = $prop[$i];
			}
		}
		if(!empty($prop)){
			$send_prop = join(",",$prefer_prop);
		}
		$data['user_id'] = $uid;
		$data['order_no'] = self::order_no();
		$data['payable_amount'] = $payable_amount;//应付商品金额
		$data['order_status'] = 0;
		$data['real_amount'] = $real_amount;//实付商品金额
		//$data['payable_freight'] = $payable_freight;//运费金额
		$data['create_time'] = date("Y-m-d H:i:s");
		$data['promotions'] = $promotions;//优惠金额
		$order_amount = $data['payable_amount']-$data['promotions'];//订单金额
		$prefer_order_amount = \Org\Oa\promotion::product_for_promotion($uid, $order_amount, 2, 1);//获取订单优惠后的金额
		$order_point = \Org\Oa\promotion::point_for_promotion($uid, $order_amount, 1);
		$data['point'] = $point + $order_point; //可获得积分
		$data['type'] = $type;
		$data['discount'] = $order_amount - $prefer_order_amount ;
		$data['order_amount'] = $prefer_order_amount ;//实际订单金额
		//return $data;exit();
		$order_send_prop = \Org\Oa\promotion::prefer_for_promotion($uid, $va['id'], 1);//判断订单是否满足增送优惠券
		if(!empty($order_send_prop)){
			if(!empty($send_prop)){
				$data['send_prop'] = $send_prop.",".$order_send_prop;
			}else{
				$data['send_prop'] = $order_send_prop;
			}
		}else{
			$data['send_prop'] = $send_prop;
		} 
		$order_id = M('order')->add($data);
		if($order_id){
			$goodsDb = M('order_goods');
			foreach ($goods as $i=>$va){
				$map['order_id'] = $order_id;
				$map['goods_id'] = $va['goods_id'];
				$map['product_id'] = $va['id'];
				if($type){
					$map['goods_price'] = $va['now_price'];
				}else{
					$map['goods_price'] = $va['sell_price'];
				}
				$map['real_price'] = $va['sell_price'];
				$map['goods_nums'] = $num[$i];
				$map['goods_weight'] = $va['weight'];
				$goodsDb->add($map);
			}
			return $data['order_no'];
		}else{
			return false;
		}
	}
	/**
	 * 生成订单号
	 */
	public static function order_no(){
		return date("YmdHis").rand(100,999);
	}
	/**
	 * 取消订单
	 */
	public static function del_order($uid,$order_no,$type){
		$data['user_id'] = $uid;
		$data['if_del'] = 0;
		$data['order_no'] = $order_no;
		$db = M('order');
		if($type == 1){
			$data['order_status'] = 0;
			$list = $db->where($data)->find();
			if(!empty($list)){
				$db->where("id='{$list['id']}'")->delete();
				M('order_goods')->where("order_id='{$list['id']}'")->delete();
				return true;
			}else{
				return false;
			}
		}elseif ($type == 2){
			$data['order_status'] = array('in','4,8');
			$list = $db->where($data)->find();
			if(!empty($list)){
				$db->where("id='{$list['id']}'")->setField('if_del',1);
				return true;
			}else{
				return false;
			}
		}
	}
	/**
	 * 删除作废订单
	 */
	public static function del_cancle_order($uid){
		$time = date("Y-m-d H:i:s",time()-7200);
		$orderDb = M('order');
		$ordergoods = M('order_goods');
		$map['is_produce'] = 0;
		$map['user_id'] = $uid;
		$map['order_status'] = 0;
		$map['pay_type'] = 0;
		$map['create_time'] = array("elt",$time);
		$res = $orderDb->where($map)->field("id")->select();
		foreach ($res as $i=>$va){
			$orderDb->where("id='{$va['id']}'")->delete();
			$ordergoods->where("order_id='{$va['id']}'")->delete();
		}
	}
	/**
	 * 自动收货七天已发货的订单
	 */
	public static function collect_collect_order($uid){
		$basc = F('web_web');
		$days = $basc['days']?$basc['days']:'7';
		$ture_time = $days*(24*3600);
		$time = date("Y-m-d H:i:s",time()-$ture_time);
		$orderDb = M('order as o');
		$where['o.order_status'] = 3;
		$where['o.user_id'] = $uid;
		$where['dd.time'] = array('elt',$time);
		$res = $orderDb->join("left join ".C('DB_PREFIX')."delivery_doc as dd on dd.order_id=o.id")->where($where)->field("o.id")->select();
		foreach ($res as $i=>$va){
			$orderDb->where("id='{$va['id']}'")->setField('order_status',4);
		}
	}
	/**
	 * 改变的订单状态
	 * $out_trade_no 订单号 $$trade_no 商家返回的订单号 $buyer_email 支付账户 $total_fee 支付金额 $pay_method 支付方式 $type
	 */
	public static function update_pay_order_status($out_trade_no,$trade_no,$buyer_email,$total_fee,$pay_method,$type){
		$db = M('order');
		$re = $db->where("order_no = '{$out_trade_no}'")->find();
		if($re['order_status'] == 0){
			$where['method_list'] = $trade_no;
			$where['user_pay_id'] = $buyer_email;
			$where['uid'] = $re['user_id'];
			$where['order_id'] = $re['id'];
			$where['pay_method'] = $pay_method;
			if($pay_method =="微信支付"){
				$where['pay_money'] = ($total_fee/100);
			}else{
				$where['pay_money'] = $total_fee;
			}
			M('user_pay_list')->add($where);
			//判断是否消费后 该用户是推广进来的
			self::update_order_status($re['order_no'],0,1);
		}
	}
	/**
	 * 改变订单的状态
	 * $order_no 订单号 $pre_status 上一个订单状态 $now_status 现在改成的状态 $note 备注
	 */
	public static function update_order_status($order_no,$pre_status,$now_status,$note=null){
		$db = M('order');
		if($now_status == 2 || $now_status == 3){
			$re = $db->where("order_no = '{$order_no}'")->find();
		}else{
			$re = $db->where("order_no = '{$order_no}' and order_status='{$pre_status}'")->find();
		}
		if(!empty($re)){
			if(!empty($note)){
				$data['note'] = $note;
			}
			if($now_status == 1){
				//付款
				$data['order_status'] = 1;
				$data['id'] = $re['id'];
				$data['pay_time'] = date("Y-m-d H:i:s");
				$db->save($data);
				//是否使用优惠券
				if($re['prop']){
					\Org\Oa\prefer::update_user_coupon($re['prop'], $re['user_id']);
				}
				
				//增加商品的销售数量
				$order_goods = M('order_goods')->where("id='{$re['order_id']}'")->field("goods_id")->select();
				foreach ($order_goods as $i=>$va){
					M('goods')->where("id='{$va['goods_id']}'")->setInc('sale_num',1);
				}
				//减库存
				self::order_del_num($re['order_id']);
				//限时抢购、团购
				if($re['type'] == 1 || $re['type'] == 2){
					//减库存
					\Org\Oa\groupbuy::groupbuy_del_num(($re['type']-1), $order_goods[0]['product_id'], $order_goods[0]['goods_nums']);
				}
				//增加用户 优惠券 用户积分
				self::add_user($re['user_id'],$re['send_prop'],$re['point']);
				//增加到资金记录表里
				self::add_funds_log($re['user_id'],$re['point'],-$re['order_amount'],1);
				
			}elseif ($now_status == 2){
				//部分发货
				$data['order_status'] = 2;
				$data['id'] = $re['id'];
				$data['send_time'] = date("Y-m-d H:i:s");
				$db->save($data);
			}elseif ($now_status == 3){
				//全部发货
				$data['order_status'] = 3;
				$data['id'] = $re['id'];
				$data['send_time'] = date("Y-m-d H:i:s");
				$db->save($data);
			}elseif ($now_status == 4){
				//完成收货
				$data['order_status'] = 4;
				$data['id'] = $re['id'];
				$data['completion_time'] = date("Y-m-d H:i:s");
				$db->save($data);
			}elseif ($now_status == 0){
				$data['order_status'] = 0;
				$data['id'] = $re['id'];
				$db->save($data);
			}
			return $re['id'];
		}else{
			return false;
		}
	}
	/**
	 * 添加订单日志
	 * $order_id 订单日志 $user 用户名 $action 动作 $note 日志
	 */
	public static function order_log($order_id,$user,$action,$note){
		$data['order_id'] = $order_id;
		$data['user'] = $user;
		$data['action'] = $action;
		$data['note'] = $note;
		$data['addtime'] = date("Y-m-d H:i:s");
		$data['result'] = "成功";
		M('order_log')->add($data);
	}
	/**
	 * 减商品库存
	 */
	public static function order_del_num($order_id){
		$order_goods = M('order_goods')->where("id='{$order_id}'")->field("product_id,goods_nums")->select();
		foreach ($order_goods as $i=>$va){
			M('products')->where("id='{$va['product_id']}'")->setDec('store_nums',$va['goods_nums']);
		}
	}
	/**
	 * 增加用户 优惠券 、用户积分
	 */
	public static function add_user($uid,$preferid=null,$point=0){
		$userDb = M('user');
		$userinfo = $userDb->where("id='{$uid}'")->field('coupon_id,point')->find();
		//增加优惠券
		if($preferid){
			if(empty($userinfo['coupon_id'])){
				$coupon_id_string = $preferid;
			}else{
				$coupon_id_string = $preferid.",".$userinfo['coupon_id'];
			}
			$user['coupon_id'] = $coupon_id_string;
		}
		//增加用户积分
		$user['point'] = $userinfo['point'] + $point;
		$user['id'] = $uid;
		$userDb->save($user);
	} 
	/**
	 * 增加用户资金记录
	 * $re 订单信息
	 */
	public static  function add_funds_log($uid,$point,$money,$type){
		$map['integral'] = $point;
		$map['type'] = $type;
		$map['money'] = $money;
		$map['add_time'] = date("Y-m-d H:i:s");
		$map['uid'] = $uid;
		M('funds_log')->add($map);
	}
	/**
	 * 修改默认地址
	 */
	public static function update_address($uid,$addressid){
		$db = M('address');
		$db->where("uid='{$uid}' and is_default='1'")->setField('is_default',0);
		$db->where("id='{$addressid}' and uid='{$uid}'")->setField('is_default',1);
	}
	/**
	 * 支付方式
	 */
	public static function paylist($pay_type=0){
		//支付宝配置
		$zhifubao = F('set_zhifubao');
		//微信支付配置
		$weixin = F('set_weixin');
		//银联支付
		$yinlian = F('set_yinlian');
		//财付通支付
		$caifutong = F('set_caifutong');
		if($zhifubao['status'] == 0){
			if($pay_type == 2){
				$res[] = array('id'=>'2','name'=>'支付宝','ck'=>1);
			}else{
				$res[] = array('id'=>'2','name'=>'支付宝','ck'=>0);
			}
			
		}
		if($weixin['status'] == 0){
			if($pay_type == 1){
				$res[] = array('id'=>'1','name'=>"微信支付",'ck'=>1);
			}else{
				$res[] = array('id'=>'1','name'=>"微信支付",'ck'=>0);
			}
		}
		if($yinlian['status'] == 0){
			if($pay_type == 3){
			   $res[] = array('id'=>'3','name'=>'银联支付','ck'=>1);
			}else{
			   $res[] = array('id'=>'3','name'=>'银联支付','ck'=>0);
			}
		}
		if($caifutong['status'] == 0){
			if($pay_type == 4){
			  $res[] = array('id'=>'4','name'=>'财付通支付','ck'=>1);
			}else{
			  $res[] = array('id'=>'4','name'=>'财付通支付','ck'=>0);
			}
		}
		return $res;
	}
}